/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeansYonet.java
 *
 * Created on Jun 16, 2012, 10:25:33 PM
 */
package sinemaproject;

import com.sun.org.apache.bcel.internal.generic.GETFIELD;
import entity.Film;
import entity.Salon;
import entity.Seans;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author USER
 */
public class SeansYonet extends javax.swing.JFrame {

    /** Creates new form SeansYonet */
    public SeansYonet() {
        initComponents();   
        refreshSeansUITable();
        showAllFilms();         
        showAllSalons();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jTextField2 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Seans No", "Salon", "Film", "Tarih", "Aktif"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setName("jPanel2"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sinemaproject.SinemaProjectApp.class).getContext().getResourceMap(SeansYonet.class);
        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setBackground(resourceMap.getColor("jButton2.background")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setBackground(resourceMap.getColor("jButton3.background")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jTextField1.setName("jTextField1"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jComboBox2.setName("jComboBox2"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, 0, 119, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, 121, Short.MAX_VALUE)
                    .addComponent(jTextField1))
                .addGap(316, 316, 316)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox1)
                        .addGap(17, 17, 17))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 693, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private EntityManagerFactory emf;
    private DefaultTableModel tmodel;
        
    private EntityManager getEntityManager() {
        if (emf == null) {
            emf = Persistence.createEntityManagerFactory("SinemaProjectPU");
        }
        return emf.createEntityManager();
    }    
    
    //Seans Ekle
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        Seans yeniSeans = new Seans();
        
        Salon salon = ((Salon)jComboBox1.getSelectedItem());
        Film film = ((Film)jComboBox2.getSelectedItem());
        
        ////yeniSeans.setSeansNo(Integer.parseInt(jTextField1.getText()));          
        yeniSeans.setSalonId(salon);          
        yeniSeans.setFilmId(film);//jComboBox1.getSelectedItem()); 
        //yeniSeans.setTarih(jTextField2.getText());
        yeniSeans.setActive(jCheckBox1.isSelected());
        
        addSeans(yeniSeans);
        refreshSeansUITable();        
    }//GEN-LAST:event_jButton1MouseClicked

    //Seans Güncelle
    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        int row = jTable1.getSelectedRow();
        Seans seans = getSeansById(jTable1.getValueAt(row, 0).toString());
        //seans.setIdSeans( getSeansById(jTable1.getValueAt(row, 0).toString()).getIdSeans() );        
        seans.setSeansNo(Integer.parseInt(jTextField1.getText()));
        seans.setSalonId( (Salon)jComboBox1.getSelectedItem() );        
        seans.setFilmId((Film)jComboBox2.getSelectedItem());  
        seans.setActive(jCheckBox1.isSelected());

        updateSeans(seans);
        refreshSeansUITable();
        
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);         
    }//GEN-LAST:event_jButton2MouseClicked

    //Seans Sil
    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        // TODO add your handling code here:
        int row = jTable1.getSelectedRow();
        Seans seans = getSeansById(jTable1.getValueAt(row, 0).toString());
        //seans.setIdSeans( getSeansById(jTable1.getValueAt(row, 0).toString()).getIdSeans() );
        
        removeSeans(seans);
        refreshSeansUITable();
        
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);        
    }//GEN-LAST:event_jButton3MouseClicked

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        // TODO add your handling code here:
        int row = jTable1.getSelectedRow();        
        //Kutuyu güncelle
        jTextField1.setText(jTable1.getValueAt(row, 1).toString());
        jComboBox1.setSelectedItem( ((Salon)jTable1.getValueAt(row, 2)) );
        jComboBox2.setSelectedItem( ((Film)jTable1.getValueAt(row, 3)) );        
        jTextField2.setText((String)jTable1.getValueAt(row, 4));    
        jCheckBox1.setSelected((Boolean)jTable1.getValueAt(row, 5));

        
        //
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);        
    }//GEN-LAST:event_jTable1MouseClicked


    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SeansYonet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SeansYonet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SeansYonet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SeansYonet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SeansYonet().setVisible(true);
            }
        });
    }
    
    public boolean addSeans(Seans seans) {
      EntityManager em = getEntityManager();
       try{
         em.getTransaction().begin();
         em.persist(seans); 
         em.getTransaction().commit();
       } finally {
        em.close();
        return false;
       }
    }    
    
    public boolean removeSeans(Seans seans) {
      EntityManager em = getEntityManager();
      try{
        em.getTransaction().begin();
        Seans tipx = em.find(Seans.class, seans.getIdSeans());
        em.remove(tipx);
        em.getTransaction().commit();
      } finally {
        em.close();
        return false;
      }
    }
    
    public boolean updateSeans(Seans seans) {
      EntityManager em = getEntityManager();
      try{
        em.getTransaction().begin();
        Seans seansx = em.find(Seans.class, seans.getIdSeans());
        
        seansx.setSeansNo(seans.getSeansNo());
        seansx.setSalonId(seans.getSalonId());
        seansx.setFilmId(seans.getFilmId());
        seansx.setTarih(seans.getTarih());
        seansx.setActive(seans.getActive());

        em.getTransaction().commit();
      } finally {
        em.close();
        return false;
      }
    }
    
    public List<Seans> getAllSeanses(){        
        List<Seans> seans;        
        EntityManager em = getEntityManager();
        try{
            em.getTransaction().begin();
            seans = em.createNamedQuery("Seans.findAll").getResultList();
        } finally {
            em.close();
        }                
        return seans;        
    }
    
    public void refreshSeansUITable(){
        
        List<Seans> seanses;
        seanses = getAllSeanses();
        
        tmodel = (DefaultTableModel) jTable1.getModel();
        //clean tmodel...
        tmodel.setRowCount(0);
        
        
        for(Seans seans : seanses){
            
            tmodel.addRow(new Object[]{seans.getIdSeans(), seans.getSeansNo(), seans.getSalonId(), seans.getFilmId(), seans.getTarih(),
                        seans.getActive()});
        }

        jTable1.setModel(tmodel);
    }    
       
    public void showAllSalons(){        
        List<Salon> salons;        
        EntityManager em = getEntityManager();
        try{
            em.getTransaction().begin();
            salons = em.createNamedQuery("Salon.findAll").getResultList();
        } finally {
            em.close();
        }                
        
        for( Salon salon : salons ){
            jComboBox1.addItem(salon);
        }
    }    
    
    public void showAllFilms(){        
        List<Film> films;        
        EntityManager em = getEntityManager();
        try{
            em.getTransaction().begin();
            films = em.createNamedQuery("Film.findAll").getResultList();
        } finally {
            em.close();
        }                
        
        for( Film film : films ){
            jComboBox2.addItem(film);
        }
    }      
    
    private Seans getSeansById(String selectedItem) {
        
        Seans seans;        
        EntityManager em = getEntityManager();
        try{
            em.getTransaction().begin();
            int id = Integer.parseInt(selectedItem);
            seans = (Seans) em.createNamedQuery("Seans.findByIdSeans").setParameter("idSeans", id).getSingleResult();
        } finally {
            em.close();
        }                
        return seans;          
    }        
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
